package com.ruoyi.framework.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pay.domain.PayCoinUserForm;
import com.ruoyi.pay.service.IPayCoinUserService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqPlayOrder;
import com.ruoyi.wq.service.IWqPlayOrderService;

@Component
public class PlayOrderService {
	
	@Autowired
	private IWqPlayOrderService wqPlayOrderService;
	
	@Autowired
    private IPayCoinUserService payCoinUserService;
	
	@Autowired
	private ISysUserService sysUserService;

	public WqPlayOrder savePlayOrder(WqPlayOrder wqPlayOrder) {
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		if(!"0".equals(user.getPlayStatus())){
			throw new ServiceException("只有老板可以下单");
		}
		if(StringUtils.isBlank(user.getWechat())){
    		throw new ServiceException("请到个人主页完善微信号");
    	}
		Integer amount = payCoinUserService.getAmount();
		Integer coinNum = wqPlayOrder.getPayCoin();
		if(coinNum > amount){
			// 趣豆不足
			throw new ServiceException("下单陪玩需要【"+coinNum+"】趣豆，请充值",888);
		}
		
		if(wqPlayOrder.getShareUserId() == null){
			wqPlayOrder.setShareUserId(user.getVisitUserId());
		}
		wqPlayOrder.setUserId(user.getUserId());
		wqPlayOrderService.saveOrUpdate(wqPlayOrder);
		
		PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		payCoinUserForm.setUserId(SecurityUtils.getUserId());
		payCoinUserForm.setCoinNum(coinNum);
		payCoinUserForm.setCoinType("1");
		payCoinUserForm.setSourceId(wqPlayOrder.getId());
		payCoinUserForm.setRemark("下单陪玩消费");
		payCoinUserService.savePayCoinUser(payCoinUserForm);
		
		return wqPlayOrder;
	}
	
}
